package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("RulefieldDAO")
@Transactional
public class RulefieldDAOImpl extends AbstractJpaDao<Rulefield> implements RulefieldDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Rulefield.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public RulefieldDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Rulefield findRulefieldById(Integer id) throws DataAccessException {

		return findRulefieldById(id, -1, -1);
	}

	@Transactional
	public Rulefield findRulefieldById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findRulefieldById", startResult, maxRows, id);
			return (Rulefield) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Rulefield> findRulefieldByName(String name) throws DataAccessException {

		return findRulefieldByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulefield> findRulefieldByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findRulefieldByName", startResult, maxRows, name);
		return new LinkedHashSet<Rulefield>(query.getResultList());
	}

	@Transactional
	public Set<Rulefield> findRulefieldByNameContaining(String name) throws DataAccessException {

		return findRulefieldByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulefield> findRulefieldByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findRulefieldByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Rulefield>(query.getResultList());
	}

	@Transactional
	public Set<Rulefield> findRulefieldByEntityclassname(String entityclassname) throws DataAccessException {

		return findRulefieldByEntityclassname(entityclassname, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulefield> findRulefieldByEntityclassname(String entityclassname, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findRulefieldByEntityclassname", startResult, maxRows, entityclassname);
		return new LinkedHashSet<Rulefield>(query.getResultList());
	}

	@Transactional
	public Set<Rulefield> findRulefieldByEntityclassnameContaining(String entityclassname) throws DataAccessException {

		return findRulefieldByEntityclassnameContaining(entityclassname, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulefield> findRulefieldByEntityclassnameContaining(String entityclassname, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findRulefieldByEntityclassnameContaining", startResult, maxRows,
				entityclassname);
		return new LinkedHashSet<Rulefield>(query.getResultList());
	}

	@Transactional
	public Set<Rulefield> findRulefieldByEntityfieldname(String entityfieldname) throws DataAccessException {

		return findRulefieldByEntityfieldname(entityfieldname, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulefield> findRulefieldByEntityfieldname(String entityfieldname, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findRulefieldByEntityfieldname", startResult, maxRows, entityfieldname);
		return new LinkedHashSet<Rulefield>(query.getResultList());
	}

	@Transactional
	public Set<Rulefield> findRulefieldByEntityfieldnameContaining(String entityfieldname) throws DataAccessException {

		return findRulefieldByEntityfieldnameContaining(entityfieldname, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulefield> findRulefieldByEntityfieldnameContaining(String entityfieldname, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findRulefieldByEntityfieldnameContaining", startResult, maxRows,
				entityfieldname);
		return new LinkedHashSet<Rulefield>(query.getResultList());
	}

	@Transactional
	public Set<Rulefield> findRulefieldByDescription(String description) throws DataAccessException {

		return findRulefieldByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulefield> findRulefieldByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findRulefieldByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Rulefield>(query.getResultList());
	}

	@Transactional
	public Set<Rulefield> findRulefieldByDescriptionContaining(String description) throws DataAccessException {

		return findRulefieldByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulefield> findRulefieldByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findRulefieldByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Rulefield>(query.getResultList());
	}

	@Transactional
	public Rulefield findRulefieldByPrimaryKey(Integer id) throws DataAccessException {
		return findRulefieldByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Rulefield findRulefieldByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findRulefieldByPrimaryKey", startResult, maxRows, id);
			return (Rulefield) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Rulefield> findRulefieldsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findRulefieldsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Rulefield> findAllRulefields() throws DataAccessException {

		return findAllRulefields(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulefield> findAllRulefields(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllRulefields", startResult, maxRows);
		return new LinkedHashSet<Rulefield>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Rulefield> findRulefieldsByRule(Integer ruleId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllRulefieldsByRule", start, max, ruleId);
		return new LinkedHashSet<Rulefield>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Rulefield> findRulefieldByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Rulefield>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Rulefield> findRulefieldsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Rulefield.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Rulefield> rulefields = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return rulefields;
	}

	@Transactional
	public Rulefield findRulefieldByPhysicalPrimaryKey(Integer ruleId, String entityclassname, String entityfieldname)
			throws DataAccessException {
		return findRulefieldByPhysicalPrimaryKey(ruleId, entityclassname, entityfieldname, -1, -1);
	}

	@Transactional
	public Rulefield findRulefieldByPhysicalPrimaryKey(Integer ruleId, String entityclassname, String entityfieldname,
			int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findRulefieldByPhyscialPrimaryKey", startResult, maxRows, ruleId,
					entityclassname, entityfieldname);
			return (Rulefield) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Rulefield entity) {
		return true;
	}
}
